package com.gcsoftware.web.mapper;

import com.gcsoftware.web.beans.T_GW_DSITEM;
import com.gcsoftware.web.beans.T_CON_DSITEM_IPARAMS;
import com.gcsoftware.web.beans.returnPage.ProtectItem;
import com.gcsoftware.web.mapper.providers.DsiInterfsParamsProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;

/**
 * Created by qs on 2018/3/14.
 */
@Mapper
public interface T_GW_DSITEMMAPPER {
    @Select("<script>SELECT C_DS_ITEM_ID ,C_DS_ITEM_NAME ,C_DS_ITEM_REMARK  FROM T_GW_DSITEM WHERE 1 = 1 <when test='c_DS_ITEM_NAME != null'>AND c_DS_ITEM_NAME like  CONCAT(CONCAT('%', #{c_DS_ITEM_NAME, jdbcType=VARCHAR}), '%')</when> ORDER BY D_MODIFY_TIME DESC </script>")
    public List<T_GW_DSITEM> queryForList(@Param(value = "c_DS_ITEM_NAME") String c_DS_ITEM_NAME);

    @Select("SELECT max(C_DS_ITEM_ID) FROM T_GW_DSITEM")
    public String getMaxDsiId();

    @Select("<script>SELECT C_DS_ITEM_ID ,C_DS_ITEM_NAME ,C_DS_ITEM_REMARK  FROM T_GW_DSITEM WHERE 1 = 1 <when test='c_DS_ITEM_ID != null'>AND c_DS_ITEM_ID = #{c_DS_ITEM_ID, jdbcType=VARCHAR}</when> <when test='c_DS_ITEM_NAME != null'>AND c_DS_ITEM_NAME = #{c_DS_ITEM_NAME, jdbcType=VARCHAR}</when></script>")
    public T_GW_DSITEM getGwDsiItem(@Param(value = "c_DS_ITEM_ID") String c_DS_ITEM_ID , @Param(value = "c_DS_ITEM_NAME") String c_DS_ITEM_NAME);

    @Insert("INSERT INTO T_GW_DSITEM (C_DS_ITEM_ID, C_DS_ITEM_NAME,C_DS_ITEM_REMARK) VALUES (#{c_DS_ITEM_ID},#{c_DS_ITEM_NAME},#{c_DS_ITEM_REMARK})")
    public int insertDsItem(T_GW_DSITEM dsitem);

    @Update("UPDATE T_GW_DSITEM SET D_MODIFY_TIME=CURRENT_TIMESTAMP , C_DS_ITEM_NAME= #{c_DS_ITEM_NAME},C_DS_ITEM_REMARK = #{c_DS_ITEM_REMARK} WHERE C_DS_ITEM_ID = #{c_DS_ITEM_ID}")
    public int updateDsItem(T_GW_DSITEM dsitem);

    @Delete("DELETE FROM T_GW_DSITEM WHERE C_DS_ITEM_ID = #{c_DS_ITEM_ID}")
    public int deleteDsItem(@Param(value = "c_DS_ITEM_ID") String c_DS_ITEM_ID);

    @Delete("DELETE T_CON_DSITEM_IPARAMS WHERE c_DS_ITEM_ID = #{c_DS_ITEM_ID}")
    public int deleteDsIParams(@Param(value = "c_DS_ITEM_ID") String c_DS_ITEM_ID);

    @Insert("INSERT INTO T_CON_DSITEM_IPARAMS (c_DS_ITEM_ID,c_INTERF_ID,c_IPARAMS_ID,c_IPARAMS_SENS) VALUES (#{c_DS_ITEM_ID},#{c_INTERF_ID},#{c_IPARAMS_ID},#{c_IPARAMS_SENS})")
    public int insertDsIparams(T_CON_DSITEM_IPARAMS dsIparam);

    @InsertProvider(type = DsiInterfsParamsProvider.class, method = "batchInsert")
    public int batchInsertDsIparams(List<T_CON_DSITEM_IPARAMS> dsIparam);

    @Select("SELECT C_DS_ITEM_ID,c_DS_ITEM_NAME FROM T_GW_DSITEM dsi WHERE EXISTS (SELECT 1 FROM T_CON_ROLE_DSITEM crd WHERE crd.C_DS_ITEM_ID = dsi.C_DS_ITEM_ID and crd.C_ROLE_ID = #{c_ROLE_ID})")
    List<T_GW_DSITEM> itemListForRole(@Param(value = "c_ROLE_ID") String c_ROLE_ID);

    @Select("SELECT C_APP_NAME FROM T_GW_APPS WHERE C_APP_STATUS = 'APP_STATUS_PUBLISH'")
    List<String> getApps();

    @Select("SELECT C_INTERF_NAME FROM T_GW_INTERFS WHERE C_INTERF_STATUS = 'INTERF_STATUS_PUBLISH'")
    List<String> getApis();

    @Select("SELECT C_DS_ITEM_NAME FROM T_GW_DSITEM")
    List<String> getSensitivityItems();

    @Select("SELECT COUNT(*) FROM T_GW_INTERFS WHERE C_INTERF_STATUS = 'INTERF_STATUS_PUBLISH'")
    int interfaceCount();

    @Select("SELECT COUNT(*) FROM T_GW_APPS WHERE C_APP_STATUS = 'APP_STATUS_PUBLISH'")
    int clientApp();

    @Select("SELECT C_DS_ITEM_NAME FROM T_GW_DSITEM")
    List<String> sensitiveItems();

    @Select("SELECT COUNT(DISTINCT C_IPARAMS_ID) FROM T_CON_DSITEM_IPARAMS")
    int protectItemCount();

    @Select("SELECT t1.C_DS_ITEM_ID, t1.C_DS_ITEM_NAME, t2.NUM\n" +
            "FROM T_GW_DSITEM t1,\n" +
            "  (SELECT C_DS_ITEM_ID, COUNT(C_IPARAMS_ID) AS NUM FROM T_CON_DSITEM_IPARAMS GROUP BY C_DS_ITEM_ID) t2\n" +
            "WHERE t1.C_DS_ITEM_ID = t2.C_DS_ITEM_ID")
    List<ProtectItem> protectItemsGroupBy();
}
